package com.example.doubt.dialog.global;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Toast;

import com.example.doubt.R;
import com.mxin.common.log.LogA;
import com.mxin.log.Logt;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * log日志
 * 显示或关闭log日志的全局浮窗
 *
 */
public class LogConfigActivity extends AppCompatActivity {

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_log_config);
	}

	//显示全局浮窗（Log）
	public void showLogViewService(View view) {
		if (LogGlobalService.isStarted) {
			return;
		}
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//判断系统版本
			if (!Settings.canDrawOverlays(this)) {
				Toast.makeText(this, "当前无权限，请授权", Toast.LENGTH_SHORT);
				startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 0);
			} else {
				startLogService();
			}
		} else {
			startLogService();
		}

	}

	private void startLogService(){
		startService(new Intent(this, LogGlobalService.class));
		LogA.subscribe(LogGlobalViewManager.getInstance());
	}

	//关闭全局浮窗（Log）
	public void dismissLogViewService(View view) {
		stopService(new Intent(this, LogGlobalService.class));
		LogA.unSubscribe(LogGlobalViewManager.getInstance());
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
		if(requestCode == 0){
			if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
				return;
			}
			if (!Settings.canDrawOverlays(this)) {
				Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
				startLogService();
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
